//use actix_web::error::ResponseError;
//use deadpool_postgres::PoolError;
use serde::{Deserialize, Serialize};
use std::error::Error as CommonError;
use std::fmt;
use std::fmt::{Display, Formatter};

#[derive(Debug, Deserialize, Serialize)]
pub struct Res<T> {
    pub data: T,
}

impl<T> Res<T> {
    pub fn new(val: T) -> Self {
        Self { data: val }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Note {
    pub id: u32,
    pub version: u32,
    pub content: String,
}
impl Note {
    pub fn has_id(&self) -> bool {
        self.id != 0
    }
}
impl Display for Note {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        write!(formatter, "content:{}", self.content)
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct NoteCategory {
    pub id: String,
    pub parent_id: String,
    pub title: String,
    pub children: Vec<NoteCategory>,
    pub created_date: i64,
}
#[derive(Debug)]
pub struct ModelError {
    pub message: String,
}
impl Display for ModelError {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        write!(formatter, "{}", self.message)
    }
}
impl CommonError for ModelError {}
impl ModelError {
    fn new(msg: &str) -> Self {
        Self {
            message: msg.to_owned(),
        }
    }
}
impl NoteCategory {
    pub fn new() -> Self {
        Self {
            ..Default::default()
        }
    }
    pub fn title(&mut self, val: &str) -> &mut Self {
        self.title = val.to_owned();
        self
    }
    pub fn id(&mut self, val: &str) -> &mut Self {
        self.id = val.to_owned();
        self
    }
    pub fn parent_id(&mut self, val: &str) -> &mut Self {
        self.parent_id = val.to_owned();
        self
    }
    pub fn children(&mut self, val: &[NoteCategory]) -> &mut Self {
        self.children = val.to_owned();
        self
    }
    pub fn check_error(&self) -> Result<(), ModelError> {
        if self.id.is_empty() && !self.children.is_empty() {
            return Err(ModelError::new(
                "当category的Id为空时，不能存在children数据",
            ));
        }
        Ok(())
    }
}

fn to_note_category_list(
    note_category: &NoteCategory,
    vec1: &mut Vec<NoteCategory>,
    parent_id: &str,
) {
    vec1.push((*note_category.to_owned().parent_id(parent_id)).to_owned());
    for n in &note_category.children {
        to_note_category_list(n, vec1, &note_category.id)
    }
}
impl From<&NoteCategory> for Vec<NoteCategory> {
    fn from(val: &NoteCategory) -> Self {
        let vec1: &mut Vec<NoteCategory> = &mut Vec::<NoteCategory>::new();
        to_note_category_list(val, vec1, &val.id);
        (*vec1).to_owned()
    }
}
#[derive(Clone, Debug, Deserialize)]
pub struct Db {
    pub dbname: String,
    pub user: String,
    pub host: String,
    pub port: u16,
}
#[derive(Clone, Debug, Deserialize)]
pub struct ConfigData {
    pub db: Db,
    pub default_port: u16,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct NoteProfile {
    pub id: u32,
    pub title: String,
}
#[derive(Debug, Deserialize)]
pub struct GetNoteQuery {
    pub id: u32,
}
#[derive(Debug, Deserialize)]
pub struct DeleteNoteQuery {
    pub id: u32,
}
